package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.UtilidadesDAO;
import org.trandio.dao.UtilidadesInterface;
import org.trandio.hb.TbAcompanante;
import org.trandio.hb.TbDiagnostico;
import org.trandio.hb.TbFechanoagenda;
import org.trandio.hb.TbPrestador;
import org.trandio.util.Utiles;

/**
 *
 * @author root
 */
public class UtilServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {


            switch (Integer.parseInt(request.getParameter("op"))) {
                case 1:


                    UtilidadesInterface ut = new UtilidadesDAO();

                    List<TbFechanoagenda> fna = new ArrayList<TbFechanoagenda>();

                    String fechaAg = request.getParameter("fecha");


                    //convierte una fecha string a la clase Date
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    Date fecP = null;

                    try {
                        fecP = df.parse(fechaAg);


                    } catch (ParseException ex) {
                        Logger.getLogger(SetDetalles.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if (!fechaAg.equals("")) {
                        fna = ut.traerFechasNoAgenda(fecP);
                    }

                    Iterator itFna = fna.iterator();

                    String respt = "";
                    respt = "[";
                    while (itFna.hasNext()) {
                        TbFechanoagenda prodFna = (TbFechanoagenda) itFna.next();
                        respt += "{\"fecha\":\"" + prodFna.getFecha() + "\"},";
                    }
                    respt = respt.substring(0, respt.length() - 1);
                    respt += "]";
                    out.println(respt);


                    break;





                case 2:
                    Utiles util = new Utiles();
                    UtilidadesInterface uti = new UtilidadesDAO();

                    List<TbFechanoagenda> fnat = new ArrayList<TbFechanoagenda>();

                    fnat = uti.traerFechasNoAgenda();

                    Iterator itFnat = fnat.iterator();

                    String resptt = "";
                    resptt = "[";
                    while (itFnat.hasNext()) {
                        TbFechanoagenda prodFna = (TbFechanoagenda) itFnat.next();

                        String[] fec = Utiles.split(prodFna.getFecha().toString(), "-");
                        resptt += "\"" + Integer.parseInt(fec[2]) + '/' + Integer.parseInt(fec[1]) + '/' + fec[0] + "\",";

                    }
                    resptt = resptt.substring(0, resptt.length() - 1);
                    resptt += "]";
                    System.out.println(resptt);

                    out.println(resptt);




                    break;

                case 3:

                    break;

                case 4:
                    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                    Date date = new Date();
                    out.println("{\"fecha\":\"" + dateFormat.format(date) + "\"}");
                    break;

                case 5:
                    int req = Integer.parseInt(request.getParameter("req"));
                    int tdes = Integer.parseInt(request.getParameter("tdes"));
                    int tpru = Integer.parseInt(request.getParameter("tpru"));



                    break;

                case 6:

                    int usr = Integer.parseInt(request.getParameter("usr"));
                    String clave = request.getParameter("clave");


                    break;

                case 7:

                    int id = Integer.parseInt(request.getParameter("id"));
                    String msg = request.getParameter("msg");

                    if (!msg.equals("")) {
                        break;
                    }

                case 8:
                    String login = request.getParameter("login");
                    break;


                case 9:

                    UtilidadesInterface utili = new UtilidadesDAO();
                    String busqueda = request.getParameter("term");
                    List<TbDiagnostico> dxs = new ArrayList<TbDiagnostico>();

                    if (!busqueda.equals("")) {
                        dxs = utili.traerDiagnosticos(busqueda);
                    }


                    Iterator itDx = dxs.iterator();

                    String resp = "";
                    resp = "[";
                    while (itDx.hasNext()) {
                        TbDiagnostico prodDx = (TbDiagnostico) itDx.next();
                        resp += "{\"id\":\"" + prodDx.getCodigo() + "\",\"label\":\"" + prodDx.getDescripcion() + "\",\"value\":\"" + prodDx.getDescripcion() + "\"},";
                    }
                    resp = resp.substring(0, resp.length() - 1);
                    resp += "]";
                    out.println(resp);
                    break;

                case 10:

                    UtilidadesInterface utildx = new UtilidadesDAO();
                    TbDiagnostico dx = new TbDiagnostico();
                    dx = utildx.traerDiagnostico(request.getParameter("cod"));


                    if (dx != null) {

                        out.println("{\"codigo\":\"" + dx.getCodigo()
                                + "\",\"id\":\"" + dx.getId()
                                + "\",\"descripcion\":\"" + dx.getDescripcion()
                                + "\",\"error\":\"" + 0 + "\"}");
                    } else {
                        out.println("{\"error\":\"1\"}");
                    }

                    break;


                case 11:
                    UtilidadesInterface utilcdx = new UtilidadesDAO();
                    String cbusqueda = request.getParameter("term");
                    List<TbDiagnostico> cdxs = new ArrayList<TbDiagnostico>();

                    if (!cbusqueda.equals("")) {
                        cdxs = utilcdx.traerDiagnosticoCodigo(cbusqueda);
                    }


                    Iterator itcDx = cdxs.iterator();

                    String cresp = "";
                    cresp = "[";
                    while (itcDx.hasNext()) {
                        TbDiagnostico prodDx = (TbDiagnostico) itcDx.next();
                        cresp += "{\"id\":\"" + prodDx.getCodigo() + "\",\"label\":\"" + prodDx.getDescripcion() + "\",\"value\":\"" + prodDx.getCodigo() + "\"},";
                    }
                    cresp = cresp.substring(0, cresp.length() - 1);
                    cresp += "]";
                    out.println(cresp);
                    break;


                case 12:
                    UtilidadesInterface utilips = new UtilidadesDAO();
                    String busquedaips = request.getParameter("term");
                    List<TbPrestador> ips = new ArrayList<TbPrestador>();

                    if (!busquedaips.equals("")) {
                        ips = utilips.traerPrestadores(busquedaips);
                    }


                    Iterator itIps = ips.iterator();

                    String respips = "";
                    respips = "[";
                    while (itIps.hasNext()) {
                        TbPrestador prodDx = (TbPrestador) itIps.next();
                        respips += "{\"id\":\"" + prodDx.getCodigo() + "\",\"label\":\"" + prodDx.getRazonSocial() + "\",\"value\":\"" + prodDx.getRazonSocial() + "\"},";
                    }
                    respips = respips.substring(0, respips.length() - 1);
                    respips += "]";
                    out.println(respips);
                    break;


                case 13:
                    UtilidadesInterface cutilips = new UtilidadesDAO();
                    String cbusquedaips = request.getParameter("term");
                    List<TbPrestador> cips = new ArrayList<TbPrestador>();

                    if (!cbusquedaips.equals("")) {
                        cips = cutilips.traerPrestadoresConsulta(cbusquedaips);
                    }


                    Iterator itcIps = cips.iterator();

                    String crespips = "";
                    crespips = "[";
                    while (itcIps.hasNext()) {
                        TbPrestador prodDx = (TbPrestador) itcIps.next();
                        crespips += "{\"id\":\"" + prodDx.getCodigo() + "\",\"label\":\"" + prodDx.getRazonSocial() + "\",\"value\":\"" + prodDx.getRazonSocial() + "\"},";
                    }
                    crespips = crespips.substring(0, crespips.length() - 1);
                    crespips += "]";
                    out.println(crespips);
                    break;

                case 14:
                    UtilidadesInterface utilipscod = new UtilidadesDAO();
                    TbPrestador ipscod = new TbPrestador();
                    ipscod = utilipscod.traerPrestadorCodigo(request.getParameter("cod"));


                    if (ipscod != null) {

                        out.println("{\"codigo\":\"" + ipscod.getCodigo()
                                + "\",\"id\":\"" + ipscod.getId()
                                + "\",\"descripcion\":\"" + ipscod.getRazonSocial()
                                + "\",\"error\":\"" + 0 + "\"}");
                    } else {
                        out.println("{\"error\":\"1\"}");
                    }

                    break;


                case 15:
                    UtilidadesInterface utilIde = new UtilidadesDAO();
                    String busquedaide = request.getParameter("term");

                    List<TbAcompanante> acom = new ArrayList<TbAcompanante>();

                    if (!busquedaide.equals("")) {
                        acom = utilIde.traerAcompananteIdentificacion(busquedaide);
                    }


                    Iterator itAcom = acom.iterator();

                    String resacom = "";
                    resacom = "[";
                    while (itAcom.hasNext()) {
                        TbAcompanante acompanante = (TbAcompanante) itAcom.next();
                        resacom += "{\"id\":\"" + acompanante.getIdentificacion() + "\",\"label\":\"" + acompanante.getNombres() + " " + acompanante.getApellidos() + "\",\"value\":\"" + acompanante.getNombres() + " " + acompanante.getApellidos() + "\"},";
                    }
                    resacom = resacom.substring(0, resacom.length() - 1);
                    resacom += "]";
                    out.println(resacom);
                    break;

                case 16:
                    UtilidadesInterface utilCod = new UtilidadesDAO();
                    TbAcompanante acom1 = new TbAcompanante();
                    acom1 = utilCod.traerAcompananteIdentificacionObj(request.getParameter("cod"));

                    if (acom1 != null) {
                        out.println("{\"nombres\":\"" + acom1.getNombres()
                                + "\",\"id\":\"" + acom1.getId()
                                + "\",\"apellidos\":\"" + acom1.getApellidos()
                                + "\",\"identificacion\":\"" + acom1.getIdentificacion()
                                + "\",\"telefono\":\"" + acom1.getTelefono()
                                + "\",\"direccion\":\"" + acom1.getDireccion()
                                + "\",\"tipo_ide\":\"" + acom1.getIdTipoIdentificacion()
                                + "\",\"error\":\"" + 0 + "\"}");
                    } else {
                        out.println("{\"error\":\"1\"}");
                    }

                    break;

                case 17:
                    UtilidadesInterface utilNom = new UtilidadesDAO();
                    String busquedanom = request.getParameter("term");

                    List<TbAcompanante> acomnom = new ArrayList<TbAcompanante>();

                    if (!busquedanom.equals("")) {
                        acomnom = utilNom.traerAcompananteNombre(busquedanom);
                    }


                    Iterator itAcomnom = acomnom.iterator();

                    String resacomnom = "";
                    resacomnom = "[";
                    while (itAcomnom.hasNext()) {
                        TbAcompanante acompanante = (TbAcompanante) itAcomnom.next();
                        resacomnom += "{\"id\":\"" + acompanante.getIdentificacion() + "\",\"label\":\"" + acompanante.getNombres() + " " + acompanante.getApellidos() + "\",\"value\":\"" + acompanante.getNombres() + " " + acompanante.getApellidos() + "\"},";
                    }
                    resacomnom = resacomnom.substring(0, resacomnom.length() - 1);
                    resacomnom += "]";
                    out.println(resacomnom);
                    break;




            }


        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
